/**
 * 
 */
package com.miolr.module.core.api;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.AnyGetterWriter;
import org.codehaus.jackson.map.ser.BeanPropertyWriter;
import org.codehaus.jackson.map.ser.BeanSerializer;
import org.codehaus.jackson.map.ser.std.BeanSerializerBase;
import org.codehaus.jackson.type.JavaType;
import org.hibernate.LazyInitializationException;

/**
 * @author iday
 *
 */
public class HibernateBeanSerializer extends BeanSerializer {
	
	/**
	 * 
	 * @param src
	 */
	protected HibernateBeanSerializer(BeanSerializer src) {
		super(src);
	}

	/**
	 * @param src
	 */
	public HibernateBeanSerializer(BeanSerializerBase src) {
		super(src);
	}

	/**
	 * @param rawType
	 * @param properties
	 * @param filteredProperties
	 * @param anyGetterWriter
	 * @param filterId
	 */
	public HibernateBeanSerializer(Class<?> rawType,
			BeanPropertyWriter[] properties,
			BeanPropertyWriter[] filteredProperties,
			AnyGetterWriter anyGetterWriter, Object filterId) {
		super(rawType, properties, filteredProperties, anyGetterWriter, filterId);
	}

	/**
	 * @param type
	 * @param properties
	 * @param filteredProperties
	 * @param anyGetterWriter
	 * @param filterId
	 */
	public HibernateBeanSerializer(JavaType type,
			BeanPropertyWriter[] properties,
			BeanPropertyWriter[] filteredProperties,
			AnyGetterWriter anyGetterWriter, Object filterId) {
		super(type, properties, filteredProperties, anyGetterWriter, filterId);
	}

	/* (non-Javadoc)
	 * @see org.codehaus.jackson.map.ser.std.BeanSerializerBase#serializeFields(java.lang.Object, org.codehaus.jackson.JsonGenerator, org.codehaus.jackson.map.SerializerProvider)
	 */
	@Override
	protected void serializeFields(Object bean, JsonGenerator jgen,
			SerializerProvider provider) throws IOException,
			JsonGenerationException {
		final BeanPropertyWriter[] props;
        if (_filteredProps != null && provider.getSerializationView() != null) {
            props = _filteredProps;
        } else {
            props = _props;
        }
        int i = 0;
        try {
            for (final int len = props.length; i < len; ++i) {
                BeanPropertyWriter prop = props[i];
                if (prop != null) { // can have nulls in filtered list
                    try {
						prop.serializeAsField(bean, jgen, provider);
					} catch (Exception e) {
						if (e.getCause() instanceof LazyInitializationException) {
							continue;
						};
						throw e;
					}
                }
            }
            if (_anyGetterWriter != null) {
                _anyGetterWriter.getAndSerialize(bean, jgen, provider);
            }
        } catch (Exception e) {
            String name = (i == props.length) ? "[anySetter]" : props[i].getName();
            wrapAndThrow(provider, e, bean, name);
        } catch (StackOverflowError e) {
            /* 04-Sep-2009, tatu: Dealing with this is tricky, since we do not
             *   have many stack frames to spare... just one or two; can't
             *   make many calls.
             */
            JsonMappingException mapE = new JsonMappingException("Infinite recursion (StackOverflowError)");
            String name = (i == props.length) ? "[anySetter]" : props[i].getName();
            mapE.prependPath(new JsonMappingException.Reference(bean, name));
            throw mapE;
        }
//		super.serializeFields(bean, jgen, provider);
	}

}
